//
//  WaybillModel.m
//  express
//
//  Created by dd on 12/7/14.
//  Copyright (c) 2014 yunqi. All rights reserved.
//

#import "Waybill.h"
#import "WaybillStatus.h"
#import "DXKit.h"

@implementation Waybill

- (instancetype)initWithData:(NSMutableDictionary *)data
{
    self = [super init];
    if (self)
    {
        _data = data;
    }
    
    return self;
}

- (id)objectForKey:(NSString *)key
{
    if (![[_data allKeys] containsObject:key]) return nil;
    
    return _data[key];
}

- (id)objectForKeyedSubscript:(NSString *)key
{
    if (![[_data allKeys] containsObject:key]) return nil;
    
    return _data[key];
}

- (NSString *)fromAndToLocation
{
    if (self.fromCustomer && self.toCustomer)
    {
        return [NSString stringWithFormat:@"%@ -> %@", self.fromCustomer.city, self.toCustomer.city];
    }
    return @"";
}

- (NSArray *)statusList
{
    NSArray *rawStatusList = _data[@"status_list"];
    NSMutableArray *statusList = [[NSMutableArray alloc] init];
    for (NSDictionary * rawStatus in rawStatusList)
    {
        WaybillStatus *status = [[WaybillStatus alloc] initWithData:rawStatus];
        [statusList addObject:status];
    }
    
    WaybillStatus *status = [[WaybillStatus alloc] initWithData:@{@"created": [NSNumber numberWithInteger:[self stringForKey:@"created"].integerValue], @"status": [NSNumber numberWithInteger:WAYBILL_STATUS_INIT], @"content": @""}];
    [statusList addObject:status];
    
    return statusList;
}

- (NSString *)statusName
{
    NSString *statusName = @"";
    NSString *status = _data[@"status"];
    switch (status.integerValue) {
        case WAYBILL_STATUS_INIT:               statusName = @"待取件"; break;
        case WAYBILL_STATUS_RECEIVING:          statusName = @"取件中"; break;
        case WAYBILL_STATUS_RECEIVE_SUCCESS:    statusName = @"已取件"; break;
        case WAYBILL_STATUS_RECEIVE_FAIL:       statusName = @"取件失败"; break;
        case WAYBILL_STATUS_IN_TRANSIT:         statusName = @"运输中"; break;
        case WAYBILL_STATUS_DELIVERING:         statusName = @"派件中"; break;
        case WAYBILL_STATUS_DELIVER_SUCCESS:    statusName = @"派件成功"; break;
        case WAYBILL_STATUS_DELIVER_FAIL:       statusName = @"派件失败"; break;
        case WAYBILL_STATUS_LOST:               statusName = @"快件丢失"; break;
        case WAYBILL_STATUS_FAIL:               statusName = @"其它失败"; break;
        case WAYBILL_STATUS_CANCEL:             statusName = @"已取消"; break;
        default:
            statusName = @"其它";
            break;
    }
    
    return statusName;
}


- (Courier *)receiveCourier
{
    if (!_receiveCourier)
    {
        if (![DX isNull: _data[@"receive_courier"]])
        {
            _receiveCourier = [[Courier alloc] initWithData:_data[@"receive_courier"]];
        }
    }
    
    return _receiveCourier;
}

- (NSString *)receiveCourierId
{
    return [self stringForKey:@"receive_courier_id"];
}

- (Courier *)deliverCourier
{
    if (!_deliverCourier)
    {
        if (![DX isNull: _data[@"deliver_courier"]])
        {
            _deliverCourier = [[Courier alloc] initWithData:_data[@"deliver_courier"]];
        }
    }
    
    return _deliverCourier;
}

- (NSString *)deliverCourierId
{
    return [self stringForKey:@"deliver_courier_id"];
}

- (WaybillCustomer *)toCustomer
{
    if (!_toCustomer)
    {
        if (![DX isNull: _data[@"to_customer"]])
        {
            _toCustomer = [[WaybillCustomer alloc] initWithData:_data[@"to_customer"]];
        }
    }
    
    return _toCustomer;
}

- (NSString *)toUserId
{
    return [self stringForKey:@"to_user_id"];
}

- (WaybillCustomer *)toCustomerAgent
{
    if (!_toCustomerAgent)
    {
        
        if (![DX isNull: _data[@"to_customer_agent"]])
        {
            _toCustomerAgent = [[WaybillCustomer alloc] initWithData:_data[@"to_customer_agent"]];
        }
    }
    
    return _toCustomerAgent;
}

- (NSString *)toUserAgentId
{
    return [self stringForKey:@"to_user_agent_id"];
}

- (WaybillCustomer *)fromCustomer
{
    if (!_fromCustomer)
    {
        if (![DX isNull: _data[@"from_customer"]])
        {
            _fromCustomer = [[WaybillCustomer alloc] initWithData:_data[@"from_customer"]];
        }
    }
    
    return _fromCustomer;
}

- (NSString *)fromUserId
{
    return [self stringForKey:@"from_user_id"];
}

- (NSString *)status
{
    return [self stringForKey:@"status"];
}

- (NSString *)content
{
    return [self stringForKey:@"content"];
}

- (NSString *)comment
{
    return [self stringForKey:@"comment"];
}

- (NSString *)deliverBonus
{
    return [self stringForKey:@"deliver_bonus"];
}

- (NSString *)deliverCharge
{
    return [self stringForKey:@"deliver_charge"];
}

- (NSString *)deliverRate
{
    return [self stringForKey:@"deliver_rate"];
}

- (NSString *)deliverUrgentTime
{
    return [self stringForKey:@"deliver_urgent_time"];
}

- (NSString *)deliverConfirmUser
{
    return [self stringForKey:@"deliver_confirm_user"];
}

- (NSString *)receiveBonus
{
    return [self stringForKey:@"receive_bonus"];
}

- (NSString *)receiveCharge
{
    return [self stringForKey:@"receive_charge"];
}

- (NSString *)receiveRate
{
    return [self stringForKey:@"receive_rate"];
}

- (NSString *)receiveUrgentTime
{
    return [self stringForKey:@"receive_urgent_time"];
}

- (NSString *)receiveConfirmUser
{
    return [self stringForKey:@"receive_confirm_user"];
}

- (NSString *)orgName
{
    return ![DX isNull: _data[@"org"]] ? (![DX isNull: _data[@"org"][@"name"]] ? _data[@"org"][@"name"] : @"") : @"";
}

- (NSInteger)rateType
{
    NSInteger type = 0;
    NSInteger status = self.status.integerValue;
    NSInteger receiveRate = self.receiveRate.integerValue;
    NSInteger deliverRate = self.deliverRate.integerValue;
    if (status < WAYBILL_STATUS_IN_TRANSIT)
    {
        if (status >= WAYBILL_STATUS_RECEIVE_SUCCESS && receiveRate == 0)
        {
            type = WAYBILL_RATE_TYPE_RECEIVE;
        }
    }
    else if (status > WAYBILL_STATUS_IN_TRANSIT)
    {
        if (status >= WAYBILL_STATUS_DELIVER_SUCCESS && deliverRate == 0)
        {
            type = WAYBILL_RATE_TYPE_DELIVER;
        }
    }
    
    return type;
}

- (NSInteger)receivePayedMoney
{
    NSString *money = [self stringForKey:@"receive_payed_money"];
    return money.intValue;
}

- (NSInteger)deliverPayedMoney
{
    NSString *money = [self stringForKey:@"deliver_payed_money"];
    return money.intValue;
}

@end
